/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package testCase;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;
import junit.framework.TestCase;
import csBakery.*;

/*
 *	@release Final Release
 *	@author  Chan Ka Yan
 *	@version 1.4.5
 */


public class BalanceControllerTest extends TestCase{
	
	private BalanceController controller;
	private String pricefilename = "";
	private String costfilename = "";
	private String logfilename = "";
	
	/*--------------------------------Attention-------------------------------------------------
	 * All the testDateBalance test cases are based on 
	 */
	
	public class LogStub extends LogController{
		LogStub(String logfilename){
			super(logfilename);
		}
	}
	
	@Test
	public void testDateBalance_A1(){
		//Test involve simple product name and integer value, positive value result, stock & sale
		pricefilename = "test\\pricelistA.txt";
		costfilename = "test\\costlistA.txt";
		logfilename = "test\\testlog_day_A.txt";
		PriceList.clearFile(logfilename);
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		log.addLog("ItemA", LogController.TYPE_STOCK, 20);
		log.addLog("ItemB", LogController.TYPE_STOCK, 20);
		log.addLog("ItemA", LogController.TYPE_SALE, 20);
		log.addLog("ItemB", LogController.TYPE_SALE, 20);
		
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String date = df.format(thisDate);
		
		String result = "Balance at " + date + " = $300.0";
		assertEquals(result, controller.generateDayBalance());
	}
	
	@Test
	public void testDateBalance_A2(){
		//Test involve simple product name and integer value, negative value result, stock only
		pricefilename = "test\\pricelistA.txt";
		costfilename = "test\\costlistA.txt";
		logfilename = "test\\testlog_day_A.txt";
		PriceList.clearFile(logfilename);
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		log.addLog("ItemA", LogController.TYPE_STOCK, 20);
		log.addLog("ItemB", LogController.TYPE_STOCK, 20);
		
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String date = df.format(thisDate);
		
		String result = "Balance at " + date + " = $-300.0";
		assertEquals(result, controller.generateDayBalance());
	}
	
	@Test
	public void testDateBalance_A3(){
		//Test involve simple product name and integer value, positive value result, sale only
		pricefilename = "test\\pricelistA.txt";
		costfilename = "test\\costlistA.txt";
		logfilename = "test\\testlog_day_A.txt";
		PriceList.clearFile(logfilename);
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		log.addLog("ItemA", LogController.TYPE_SALE, 20);
		log.addLog("ItemB", LogController.TYPE_SALE, 20);
		
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String date = df.format(thisDate);
		
		String result = "Balance at " + date + " = $600.0";
		assertEquals(result, controller.generateDayBalance());
	}
	
	@Test
	public void testDateBalance_B1(){
		//Test involve 1+ words name and decimal value
		//Test involve simple product name and integer, negative value result, stock only
		pricefilename = "test\\pricelistB.txt";
		costfilename = "test\\costlistB.txt";
		logfilename = "test\\testlog_day_B.txt";
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		log.addLog("Aish Merahrah", LogController.TYPE_STOCK, 20);
		log.addLog("Bara Brith bread", LogController.TYPE_STOCK, 20);
		
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String date = df.format(thisDate);
		
		String result = "Balance at " + date + " = $-264.0";
		assertEquals(result, controller.generateDayBalance());
	}
	
	@Test
	public void testDateBalance_B2(){
		//Test involve 1+ words name and decimal value
		//Test involve simple product name and integer, positive value result, sale only
		pricefilename = "test\\pricelistB.txt";
		costfilename = "test\\costlistB.txt";
		logfilename = "test\\testlog_day_B.txt";
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		log.addLog("Aish Merahrah", LogController.TYPE_SALE, 20);
		log.addLog("Bara Brith bread", LogController.TYPE_SALE, 20);
		
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String date = df.format(thisDate);
		
		String result = "Balance at " + date + " = $2424.0";
		assertEquals(result, controller.generateDayBalance());
	}
	
	@Test
	public void testDateBalance_B3(){
		//Test involve 1+ words name and decimal value
		//Test involve simple product name and integer, positive value result, stock and sale
		pricefilename = "test\\pricelistB.txt";
		costfilename = "test\\costlistB.txt";
		logfilename = "test\\testlog_day_B.txt";
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		log.addLog("Aish Merahrah", LogController.TYPE_STOCK, 20);
		log.addLog("Bara Brith bread", LogController.TYPE_STOCK, 20);
		log.addLog("Aish Merahrah", LogController.TYPE_SALE, 20);
		log.addLog("Bara Brith bread", LogController.TYPE_SALE, 20);
		
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String date = df.format(thisDate);
		
		String result = "Balance at " + date + " = $2160.0";
		assertEquals(result, controller.generateDayBalance());
	}
	
	@Test
	public void testMonthBalance_A(){
		//Test involve simple product name and integer value
		pricefilename = "test\\pricelistA.txt";
		costfilename = "test\\costlistA.txt";
		logfilename = "test\\testlog_month_A.txt";
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		
		//set date within the same month of test
		//Calendar.
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		Date testDate = new Date();
		Calendar thisCal = Calendar.getInstance();
		Calendar testCal = Calendar.getInstance();
		thisCal.setTime(testDate);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 1);
		Date thisDate = testCal.getTime();
		String date = df.format(thisDate);
		log.addLog("ItemA", LogController.TYPE_STOCK, 20, date);

		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 2);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("ItemB", LogController.TYPE_STOCK, 20, date);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 3);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("ItemC", LogController.TYPE_STOCK, 20, date);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 4);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("ItemD", LogController.TYPE_STOCK, 20, date);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 5);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("ItemE", LogController.TYPE_STOCK, 20, date);
		
		String result = "Balance at 2013/4/1 = $-100.0\n" +
				"Balance at 2013/4/2 = $-200.0\n" +
				"Balance at 2013/4/3 = $-300.0\n" +
				"Balance at 2013/4/4 = $-400.0\n" +
				"Balance at 2013/4/5 = $-500.0\n" +
				"\n===================================\n" +
				"Total Balance at " + controller.getMonthName(testCal.get(Calendar.MONTH)) + " = $-1500.0\n" +
				"Average in " + controller.getMonthName(testCal.get(Calendar.MONTH)) + " = $-300.00\n" +
				"===================================\n";
		assertEquals(result, controller.generateMonthBalance());
	}
	
	@Test
	public void testMonthBalance_B(){
		//Test involve 1+ words name and decimal value
		pricefilename = "test\\pricelistB.txt";
		costfilename = "test\\costlistB.txt";
		logfilename = "test\\testlog_month_A.txt";
		controller = new BalanceController(pricefilename, costfilename, logfilename);
		LogController log = controller.getLogController();
		
		//set date within the same month of test
		//Calendar.
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		Date testDate = new Date();
		Calendar thisCal = Calendar.getInstance();
		Calendar testCal = Calendar.getInstance();
		thisCal.setTime(testDate);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 11);
		Date thisDate = testCal.getTime();
		String date = df.format(thisDate);
		log.addLog("Bara Brith bread", LogController.TYPE_STOCK, 20, date);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 12);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("Banana Bread", LogController.TYPE_STOCK, 20, date);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 13);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("Aish Merahrah", LogController.TYPE_SALE, 20, date);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 14);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("Baguette", LogController.TYPE_STOCK, 20, date);
		
		testCal.set(thisCal.get(Calendar.YEAR), thisCal.get(Calendar.MONTH), 15);
		thisDate = testCal.getTime();
		date = df.format(thisDate);
		log.addLog("Banana Bread", LogController.TYPE_STOCK, 1, date);
		
		String result = "Balance at 2013/4/11 = $-110.0\n" +
				"Balance at 2013/4/12 = $-88.0\n" +
				"Balance at 2013/4/13 = $1414.0\n" +
				"Balance at 2013/4/14 = $-132.0\n" +
				"Balance at 2013/4/15 = $-4.4\n" +
				"\n===================================\n" +
				"Total Balance at " + controller.getMonthName(testCal.get(Calendar.MONTH)) + " = $1079.6\n" +
				"Average in " + controller.getMonthName(testCal.get(Calendar.MONTH)) + " = $215.92\n" +
				"===================================\n";
		assertEquals(result, controller.generateMonthBalance());
	}
}
